//gen.c
#include "gen.h"

void TraverseTree(node *n)
{
	int i = 0; 	//counter var

	if(strstr(n->text, "create_expr") != NULL)
	{
		currlist = CreateInstruction(n);	//create an instruction
		if(instrlist->next == NULL)
		{
			instrlist = currlist;
			currlist = NULL;
			instrlist->next = currlist;

		}
		else
		{
			currlist = currlist->next;
		}
	}//end if
	else
	{
		for(i = 0; i < n->numchild; i++)
		{
			TraverseTree(n->children[i]);	//traverses the tree
		}//end for
	}//end else
}

instruction* CreateInstruction(node *n)
{
		//creates an instruction and returns it
		//assumes that it's started at "create_expr"
	instruction *nl;
	int i = 0;
	char* temp1;
	char* temp2;
	char* temp3;
	char* select = "select ";
	char* select2;
	char* from = "from ";
	char* from2;
	char* where;

	char* as = strdup(n->children[1]->text);
		//assumes plist is one item
	char* p = strdup(n->children[3]->text);

	temp1 = GetSelect(n->children[5]);	//gets SELECT stuff
	select2 = malloc((sizeof(char))*(strlen(select)+strlen(temp1)+strlen(p)+7));
	sprintf(select2, "%s%s as %s\n", select, temp1, p);

	temp2 = GetFrom(n->children[5]);	//gets FROM stuff
	from2 = malloc((sizeof(char))*(strlen(from)+strlen(temp2)+strlen(as)+7));
	sprintf(from2, "%s%s as %s\n", from, temp2, as);

	where = GetWhere(n->children[5]);

	//combine everything
	temp3 = malloc((sizeof(char))*(strlen(select2)+strlen(from2)+strlen(where)+0));
	sprintf(temp3, "%s%s%s", select2, from2, where);

	//create new list item
	nl = malloc(sizeof(instruction));
	nl->instr = strdup(temp3);
	nl->next = NULL;


	return nl;

}

char* GetFrom(node* n)
{
	//assuming simplist case
	//assuming SQL_expression passed in
	node *from = n->children[1];	//grabs from_expr

	return strdup(from->children[1]->text);	//returns the IDENT
}

char* GetSelect(node *n)
{
	//assuming 1 item in SELECT statement
	//assuming SQL_expression is passed in
	node *select = n->children[0];	//grabs select_expr

	return strdup(select->children[1]->text);	//returns the IDENT
}

char* GetWhere(node *n)
{
	//assuming simplist case
	//assuming SQL_expression passed in
	node *where = n->children[2];	//grabs where_expr
	node *expr = where->children[1];	//grabs expr

	char* left;
	char* middle;
	char* right;
	char* temp;

	//assume selectitem is smallest possible, ie. IDENT
	left = strdup(expr->children[0]->text);		//copies left side
	middle = strdup(expr->children[1]->text);	//copes operator
	right = strdup(expr->children[2]->text);	//copies right side

	temp = malloc((sizeof(char))*(strlen(left)+strlen(middle)+strlen(right)+3));
	sprintf(temp, "where %s %s %s\n", left, middle, right);

	return temp;
}

void PrintList(instruction* l)
{
	instruction* temp;

	temp = l;

	if(temp == NULL)
	{
		printf("LIST IS EMPTY\n");
		return;
	}

	printf("SQL LIST-----------------------------------------------------\n");

	while(temp != NULL)
	{
		printf("%s", temp->instr);
		temp = temp->next;
	}

	printf("END SQL LIST-----------------------------------------------------\n\n");
}
